package com.lab02.spring.firstpage.controllers;

import com.lab02.spring.firstpage.services.ReservationService;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.ParameterizableViewController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: bsceran
 * Date: 5/23/13
 * Time: 3:51 PM
 * To change this template use File | Settings | File Templates.
 */
public class ReserveController extends ParameterizableViewController {
    private ReservationService reservationService;

    public void setReservationService(ReservationService reservationService) {
        this.reservationService = reservationService;
    }

    @Override
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        String courtName = ServletRequestUtils.getStringParameter(httpServletRequest,"courtName");
        Map<String,Object> model = new HashMap<String,Object>();

        if(courtName != null) {
            model.put("courtName", courtName);
            model.put("reservations", reservationService.query(courtName));
        }
//        else {
//            throw new ReservationNotAvailable(courtName, new Date(), 0);
//        }
        return new ModelAndView(getViewName(), model);
    }
}
